<?

//Initiate Authenticate

SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","group_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_tools_importdata.php";
$FileTemplate  = "ais_tools_importdata_list.html";
$TableName     = "tblgroup";
$SubHeaderTitle= "Import Data Management";
$HTMLFormName  = "BackupRestore";
$PageSize      = 10 ;
$HTMLGridList  = "GroupGrid";

//************ BACKUP VARIABLES ***********
$TRANSFERED_DIR = "../../transfered";

//************ FORM ACTION **************
 if (GetParam("Action","")=="Upload") {
	 if ($_FILES["import_file"]) {
		 	$Files = $_FILES["import_file"];
			copy($Files["tmp_name"],$TRANSFERED_DIR."/".ereg_replace("Backup","Ready",$Files["name"]));
			header("Location: $FileName");
			exit;
 	 }

 }elseif (GetParam("Action","")=="Delete") {
	 if (GetParam("FileName","")) {
		if (file_exists($TRANSFERED_DIR."/".GetParam("FileName",""))) {
			unlink($TRANSFERED_DIR."/".GetParam("FileName",""));
			header("Location: $FileName");
			exit;
		}
 	 }

 }elseif (GetParam("Action","")=="Import") {
	 if (GetParam("FileName","")) {
		if (file_exists($TRANSFERED_DIR."/".GetParam("FileName",""))) {
			$ShellStr = "unzip -joqq ".$TRANSFERED_DIR."/".GetParam("FileName","")." -d $TMP_DIR ";
			$output = shell_exec($ShellStr);

			$ShellStr = "mysql -u ".DB_USER." ".DB_NAME." < ".$TMP_DIR."/".ereg_replace("zip","sql",GetParam("FileName",""));
			$output = shell_exec($ShellStr);

			$ShellStr = "mv ".$TRANSFERED_DIR."/".GetParam("FileName","")." ".$TRANSFERED_DIR."/".ereg_replace("Ready","Transfered",GetParam("FileName",""));
			$output = shell_exec($ShellStr);

			unlink($TMP_DIR."/".ereg_replace("Ready","Backup",ereg_replace("zip","sql",GetParam("FileName",""))));
			header("Location: $FileName");
			exit;
		}
 	 }
 }

//Initiate Database Table For Search;
$backuptype = GetParam("backuptype","");
$fromDate = GetParam("fromDate","");
$toDate   = GetParam("toDate","");
$periodDay = GetParam("periodDay","");
//******* DATABASE FORM QUERY ***********

$SQL = "SELECT * FROM `$TableName` ";

$SQLCount = "SELECT count(*) FROM `$TableName` ";

//Retrieve Parameter from search
if ($FCode) {
	$CustomWhere = " user like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FName) {
	$CustomWhere = " $TableName.group_id = '".$FName."'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;
}

//Build  WHERE 
if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";

$SQL = $SQL.$WHERE;
//print $SQL;
//Select User Group

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$FileList = ReadFileList($TRANSFERED_DIR);
if ($FileList) asort($FileList);

$RecCnt = count($FileList);

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$FileList,'',$PageSize,$PageNum,$RecCnt);

$TBS->MergeBlock("monthformblk",GetArrayMonth());
$TBS->MergeBlock("yearformblk",GetArrayYear());
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function m_event_blg ($BlockName,&$CurrRec,&$DetailSrc,$RecNum) {
	global $FileName;

	if (ereg("Ready",$CurrRec["filename"])) { //bb/bk
		$CurrRec["action"] = "<a href=$FileName?Action=Import&FileName=".$CurrRec["filename"].">Import</a> / <a href=$FileName?Action=Delete&FileName=".$CurrRec["filename"].">Delete</a>";
	} elseif (ereg("Transfered",$CurrRec["filename"])) { //tb/tk
		$CurrRec["action"] = "<a href=$FileName?Action=Delete&FileName=".$CurrRec["filename"].">Delete</a>";
	}
}

?>
